<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * goods 表操作方法
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-02-25 13:43:37 +0800 (二, 2014-02-25) $
 * @version : $Id: goods_dao.php 31 2014-02-25 05:43:37Z spike $
 * @rev     : $Revision: 31 $
 * @url     : $HeadURL: http://192.168.0.252/tcfs/code/application/backend/daos/goods_dao.php $
 */

class Goods_dao extends Base_Goods_dao
{
    public function __construct()
    {
        parent::__construct();
    }

    // --------------------------------------------------------------------

    /**
     * [批量更新商品状态]
     * @param  [array]  $goods_sns    [商品编号数组]
     * @param  [string] $status       [修改的状态]
     * @param  [array]  $allow_status [允许修改的状态]
     * @return [int]                  [影响行数]
     */
    public function batch_change_goods_status($goods_sns, $status, $allow_status)
    {
        if(    ! is_array($goods_sns) 
            || count($goods_sns) <= 0
            || empty($status)
            || ! is_array($allow_status) 
            || count($allow_status) <= 0
        )
        {
            return 0;
        }
        $binds = array();
        $sql   = " UPDATE `{$this->table}`";
        $sql  .= " SET `status` = ?, `update_time` = ?, `update_admin` = ?";
        $binds = array($status, now_str(), login_info('admin_id'));

        $sql  .= " WHERE 1 = 1";
        $sql  .= " AND `goods_sn` IN (";
        foreach ($goods_sns as $g) 
        {
            $sql    .= " ?,";
            $binds[] = array_shift($goods_sns);
        }
        $sql   = rtrim($sql, ",");
        $sql  .= ")";

        $sql  .= " AND `status` IN (";
        foreach ($allow_status as $a) 
        {
            $sql  .= " ?,";
            $binds[] = array_shift($allow_status);
        }
        $sql   = rtrim($sql, ",");
        $sql  .= ")";

        $this->connect('write');

        $affected_row = $this->query($sql, $binds);
        return (int)$affected_row;
    }

    // --------------------------------------------------------------------

    /**
     * [返回最大的商品编码]
     * @return [string]
     */
    public function get_max_goods_sn()
    {
        $sql   = " SELECT `goods_sn` FROM `{$this->table}`";
        $sql  .= " ORDER BY right(`goods_sn`, 4) DESC LIMIT 1;";

        $this->connect('read');

        $data = $this->query($sql);
        return $data['0']['goods_sn'] ? $data['0']['goods_sn'] : '';
    }

}
/* End of file */